package com.flyoil.petromp.ui.activity.activity_repertory;

import android.content.Intent;

import com.cnpc.fyutils.ToastUtils;
import com.flyoil.petromp.Constant.ConstantNetworkCode;
import com.flyoil.petromp.R;
import com.flyoil.petromp.adapter.adapter_service.ItemSupplierAdapter;
import com.flyoil.petromp.apiservice.HttpModel;
import com.flyoil.petromp.apiservice.OnObserverRetrofitResetCallBack;
import com.flyoil.petromp.base.BaseActivity;
import com.flyoil.petromp.base.BaseEntity;
import com.flyoil.petromp.base.BaseRecyAdapter;
import com.flyoil.petromp.entity.entity_service.SupplierListEntity;
import com.flyoil.petromp.utils.HintConstants;
import com.flyoil.petromp.view.SearchDataView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供应商档案搜索界面
 */
public class SearchSupplierDataActivity extends BaseActivity {
    private SearchDataView searchDataView;
    private ItemSupplierAdapter adapter;
    private String message = "";

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_search_supplier_data;
    }

    @Override
    protected void initViews() {
        super.initViews();
        view_statusbar.setBackgroundResource(R.color.search_layout_status_d9d9d9);
        recyclerView = $(R.id.list_search_supplier_data);
        setRecyviewLayoutManager(null);
        adapter = new ItemSupplierAdapter(mContext);
        recyclerView.setAdapter(adapter);
        searchDataView = $(R.id.search_search_supplier_data);
        searchDataView.setTextHint(HintConstants.SEARCH_SUUPPLIER_HINT);
        httpModel = new HttpModel();
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        searchDataView.setOncListener(onListener, mContext);
        adapter.setOnItemClickListener(onItemClickListener);
        setOnRefreshListener(true);
    }

    private SearchDataView.OnListener onListener = new SearchDataView.OnListener() {
        @Override
        public void Onback() {
            backFinish();
        }

        @Override
        public void Onsearch(String Newmessage) {
            isFirstLoadck = true;
            pageNumber = 1;
            message = Newmessage;
            searchData();
        }
    };

    private void searchData() {
        Map<String, Object> map = new HashMap<>();
        map.put("pageNumber", pageNumber);
        map.put("pageSize", pageSize);
        map.put("nameLK", message);
        httpModel.getSupplierList(map, new OnObserverRetrofitResetCallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity model) {
                super.onSuccess(model);

                SupplierListEntity entity = null;
                if (model instanceof SupplierListEntity) {
                    entity = (SupplierListEntity) model;
                }
                if (entity != null && entity.getData().getList() != null && model.getCode() == ConstantNetworkCode.SuccedCode) {
                    if (isFirstLoadck) {
                        isFirstLoadck = false;
                        adapter.setDataList(entity.getData().getList());
                        recyclerView.notifyDataSetChanged();
                        if (entity.getData().getList().size() > 0) {
                            onDataSucceed();
                            pageNumber++;
                        } else {
                            onDataNull("未搜索到相关内容");
                        }
                    } else {
                        if (pageNumber == 1) {
                            adapter.setDataList(entity.getData().getList());
                            recyclerView.notifyDataSetChanged();
                            onRefreshDataSucceed();
                        } else {
                            adapter.addBottonDatas(entity.getData().getList());
                            recyclerView.notifyDataSetChanged();
                            if (entity.getData().getList().size() < pageSize) {
                                onLoadDataFinish();
                            } else {
                                onLoadDataSucceed();
                            }
                        }
                        pageNumber++;
                    }
                } else {
                    ToastUtils.show_toast(model.getMessage());
                    if (isFirstLoadck) {
                        onDataNull(null);
                    } else {
                        if (pageNumber == 1) {
                            onRefreshDataError();
                        } else {
                            onLoadDataError();
                        }
                    }

                }
            }

            @Override
            public void onFailure(String msg) {
                if (isFirstLoadck) {
                    onDataNull(null);
                } else {
                    if (pageNumber == 1) {
                        onRefreshDataError();
                    } else {
                        onLoadDataError();
                    }
                }
            }
        });
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        searchData();
    }

    @Override
    protected void onLoadData() {
        super.onLoadData();
        searchData();
    }

    private BaseRecyAdapter.OnItemClickListener onItemClickListener = new BaseRecyAdapter.OnItemClickListener() {
        @Override
        public void Onclick(int position, Object data) {
            Intent intent = new Intent(mContext, SupplierMessageActivity.class);
            SupplierListEntity.DataBean.ListBean entity = adapter.getItemData(position);
            intent.putExtra("id", entity.getId());
            startActivity(intent);
            finish();
        }
    };
}
